package com.zst.microservicenodeone.controller;

import com.alibaba.fastjson.JSONObject;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixException;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.zst.microservicenodeone.context.UserContext;
import com.zst.microservicenodeone.context.UserContextHolder;
import com.zst.microservicenodeone.feign.FeignDiscovery;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.validation.constraints.Null;
import java.sql.SQLException;
import java.util.List;

@RestController
@RequestMapping("/main")
public class MainController {
    private final static Logger logger = LoggerFactory.getLogger(MainController.class);

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private FeignDiscovery feignDiscovery;

    ThreadLocal<String> stringThreadLocal = new ThreadLocal<>();

    @RequestMapping("/callPolyphiaByDiscovery")
    public Object callPolyphia(){
        RestTemplate restTemplate = new RestTemplate();
        List<ServiceInstance> instanceList = discoveryClient.getInstances("polyphiaSongsService");

        if(instanceList.size() == 0){
            return null;
        }

        String serviceUrl = String.format("%s/polyphia/NewLevelsNewDevils", instanceList.get(0).getUri().toString());
        ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(serviceUrl, HttpMethod.GET, null, JSONObject.class);

        return responseEntity.getBody();
    }

    @RequestMapping("/callPolyphiaByRibbon")
    public Object callPolyphiaByRibbon(){
        ResponseEntity<JSONObject> responseEntity = this.restTemplate.exchange(
                "http://polyphiaSongsService/polyphia/NewLevelsDevils",
                HttpMethod.GET,
                null,
                JSONObject.class
        );

        return responseEntity.getBody();
    }


    @HystrixCommand(
            commandKey = "keyone",
            commandProperties = {
                    @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold", value = "10"),
                    @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds", value = "1000"),
                    @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage", value = "75")
            }

    )
    @RequestMapping("callPolyphiaByOpenFeign")
    public Object callPolyphiaByOpenFeign(){
        return feignDiscovery.getPolyphia();
    }



    public JSONObject fallbackMethod(){
        return feignDiscovery.getPolyphia();
    }

}
